package cn.tedu._03vehicle.controller;
import cn.tedu._03vehicle.base.response.statuscode;
import cn.tedu._03vehicle.mapper.vmapper;
import cn.tedu._03vehicle.base.response.jsonresulte;
import cn.tedu._03vehicle.pojo.dto.vadd;
import cn.tedu._03vehicle.pojo.entity.vehicle;
import cn.tedu._03vehicle.pojo.vo.loginvo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Tag(name="车辆管理")
@RestController
@RequestMapping("/vehicle")
public class vcontroller
{
    @Autowired
    private vmapper vmapper;
    @PostMapping("/add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public jsonresulte add(@RequestBody vadd add)
    {
        log.debug("开始添加车辆"+add);
        vehicle v=new vehicle();
        BeanUtils.copyProperties(add,v);
        v.setUserId(1L);
        v.setCreateTime(new Date());
        v.setUpdateTime(new Date());
        log.debug("开始添加车辆"+ v);
        vmapper.insert(v);
        return jsonresulte.success();

    }

    @GetMapping("/select")
    @Operation(summary = "查询车辆")
    @ApiOperationSupport(order = 20)
    @Parameters(value={
            @Parameter(name = "brand",description = "车辆品牌",required = true),
            @Parameter(name = "license",description = "车辆车牌",required = true),
            @Parameter(name = "userId",description = "用户编号",required = true),
            @Parameter(name="v", hidden = true)
    })
    public jsonresulte select(vehicle v, HttpSession  session)
    {
        log.debug("开始查询车辆"+ v);
        loginvo vo= (loginvo) session.getAttribute("user");
        if(vo==null){return new jsonresulte(statuscode.NOT_LOGIN);}
        String brand=v.getBrand();
        String license=v.getLicense();
        Long userId=v.getId();
        List<vehicle> vv = vmapper.select(brand, license, userId);
        // List<vehicle> vv = vmapper.select(vehicle v, userId);
        log.debug("结束查询车辆"+ vv);
        return jsonresulte.success(vv);
    }
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public jsonresulte delete(@PathVariable @Range(min = 1, max=30, message = "id须在1-30间") Long id, HttpSession  session)
    {
        if(id<0)
        {
            throw new RuntimeException("id不能小于0");//自定义error
        }
        log.debug("结束查询车辆id"+ id);
        loginvo vo= (loginvo) session.getAttribute("user");
        if(vo==null)
        {
            return new jsonresulte(statuscode.NOT_LOGIN);
        }
        Long userId=vo.getUserId();
        vmapper.delete(id,userId);
        return jsonresulte.success();
    }
}
